package Test_1026;

import java.util.Arrays;

/**
 * Created with Intellij IDEA
 * Description:
 * User: 星辰
 * Dare: 2024-10-26
 * Time: 13:47
 */

public class MyArrayList implements IList{
    public int[] array;
    public int usedSize;
    //默认大小
    public static final int DEFAULT_CAPACITY=10;
    public MyArrayList(){
        this.array=new int[DEFAULT_CAPACITY];
    }

    public boolean isFull(){
        return this.usedSize==array.length;
    }
    //私有的，自己用
    private void grow(){
        this.array= Arrays.copyOf(this.array,
                2*this.array.length);
    }
    private void checkPos(int pos) throws PosIllegal{
        if(pos <0 || pos >usedSize){
            throw new PosIllegal("Pos位置不合法");
        }
    }
    private void checkPos2(int pos) throws PosIllegal{
        if(pos <0 || pos >=usedSize){
            throw new PosIllegal("Pos位置不合法");
        }
    }
    @Override
    //添加
    public void add(int data) {
        if(isFull()){
            grow();
        }
      this.array[usedSize]=data;
      usedSize++;
    }

    @Override
    //插入
    /*1.不能小于0或者大于usedsize
    2.看看是否满了*/
    public void add(int pos, int data) {
        //捕捉可能出现的异常
        try{
            checkPos(pos);
            for (int i = usedSize-1; i >=pos; i--) {
                this.array[i+1]=array[i];

            }
            array[pos]=data;
            usedSize++;
        }catch(PosIllegal e){
            System.out.println("pos位置不合法");
            e.printStackTrace();
        }
        if(isFull()){
            grow();
        }


    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(array[i]==toFind)
                return true;
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(array[i] == toFind) {
                return i;
            }
        }
        return 0;
    }
private void checkEmpty(){
    if(isEmpty()){
        throw new EmptyException("顺序表为空");
    }
}
public boolean isEmpty(){
        return usedSize==0;
}
    @Override
    //获取值
    public int get(int pos) {
        try{
            checkEmpty();
            checkPos2(pos);
            return array[pos];
        }catch(PosIllegal e){
            System.out.println("pos 位置不合法");
            e.printStackTrace();
        }
        return -1;
    }
   /* public boolean Empty(){
        return usedSize==0;
    }*/
    @Override
    //替换
    public void set(int pos, int value) {
        try{
            checkEmpty();
            checkPos2(pos);
            array[pos]=value;
        }catch(PosIllegal e){
            e.printStackTrace();
        }catch (EmptyException e){
            e.printStackTrace();
        }

    }

    @Override
    public void remove(int toRemove) {
        try{
            checkEmpty();
          int pos=indexOf(toRemove);
          if(pos==-1){
              return;
          }
            for (int i = pos; i < usedSize-1; i++) {
                array[i]=array[i+1];
            }
            usedSize--;
        }catch(PosIllegal e){
            e.printStackTrace();
        }catch (EmptyException e){
            e.printStackTrace();
        }

    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
         usedSize=0;
    }

    @Override
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(array[i]+" ");
        }
    }
}
